/*
 * CartNetworkOps.java
 * 
 * Created by demory on Mar 28, 2009, 5:36:25 PM
 * 
 * Copyright 2008 David D. Emory
 * 
 * This file is part of Five Points. See <http://www.fpdev.org> for
 * additional information regarding the project.
 * 
 * Five Points is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * Five Points is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with Five Points.  If not, see <http://www.gnu.org/licenses/>.
 */

package org.fpdev.apps.cart.modules;

import org.fpdev.apps.cart.*;
import java.awt.Color;
import java.awt.geom.Point2D;
import javax.swing.JOptionPane;
import org.fpdev.apps.cart.actions.CreateAnchorPointAction;
import org.fpdev.apps.cart.actions.CreateCorridorAction;
import org.fpdev.apps.cart.actions.DeleteLineAction;
import org.fpdev.apps.cart.actions.EditLinePropertiesAction;
import org.fpdev.apps.cart.actions.ModifyLineAction;
import org.fpdev.apps.cart.actions.CreateLineAction;
import org.fpdev.apps.cart.actions.DeleteAnchorPointAction;
import org.fpdev.apps.cart.actions.DeleteCorridorAction;
import org.fpdev.apps.cart.actions.FlipCorridorAction;
import org.fpdev.apps.cart.actions.MoveAnchorPointAction;
import org.fpdev.apps.cart.actions.SetLineStyleAction;
import org.fpdev.apps.cart.network.AnchorPoint;
import org.fpdev.apps.cart.network.Corridor;
import org.fpdev.apps.cart.network.Line;
import org.fpdev.apps.cart.network.LineStyle;

/**
 *
 * @author demory
 */
public class NetworkOps extends CartModule {

  public NetworkOps(Cartographer cart) {
    super(cart);
  }

  public ID getID() {
    return ID.NETWORK;
  }

  public CartEventResult handleEvent(CartEvent event) {

    Editor ed = event.getEditor();
    double x = event.getDoubleProperty("x"), y = event.getDoubleProperty("y");
    
    //Editor ed = cart_.getEditor(event.getIntProperty("eid"));
    
    CartEventResult result = new CartEventResult();

    switch(event.getType()) {
      case CREATE_ANCHOR:
        result.setAction(new CreateAnchorPointAction(ed, x, y));
        return result;
      case DELETE_ANCHOR:
        result.setAction(new DeleteAnchorPointAction(ed, x, y));
        return result;
      case MOVE_ANCHOR:
        AnchorPoint anchor = (AnchorPoint) event.getProperty("anchor");
        Point2D.Double from = (Point2D.Double) event.getProperty("from");
        Point2D.Double to = (Point2D.Double) event.getProperty("to");
        result.setAction(new MoveAnchorPointAction(ed, anchor, from, to));
        return result;
      case CORRIDOR_ENDPOINT:
        result.setAction(corridorEndpointEvent(ed, x, y));
        return result;
      case DELETE_CORRIDOR:
        result.setAction(new DeleteCorridorAction(ed, ed.getDocument().getNetwork().getCorridorAtXY(x, y,
          ed.getFrame().getCanvas().getClickToleranceW())));
        return result;
      case FLIP_CORRIDOR:
        Corridor corr = ed.getDocument().getNetwork().getCorridorAtXY(x, y, ed.getFrame().getCanvas().getClickToleranceW());
        if(corr != null) result.setAction(new FlipCorridorAction(ed, corr));
        return result;
      case CREATE_LINE:
        result.setAction(newLineEvent(ed));
        return result;
      case DELETE_LINE:
        result.setAction(deleteLineEvent(ed));
        return result;
      case MODIFY_LINE:
        result.setAction(modifyLineEvent(ed, x, y));
        return result;
      case SET_LINE_STYLE:
        Line line = (Line) event.getProperty("line");
        LineStyle style = (LineStyle) event.getProperty("style");
        result.setAction(new SetLineStyleAction(ed, line, style));
        return result;
    }
    return null;
  }

  private AnchorPoint fromAnchor_;

  public CartAction corridorEndpointEvent(Editor ed, double x, double y) {
    AnchorPoint pt = ed.getDocument().getNetwork().getPointAtXY(x, y,
            ed.getFrame().getCanvas().getClickToleranceW());
    if(pt == null) return null;

    if(fromAnchor_ == null) {
      fromAnchor_ = pt;
      ed.getFrame().getCanvas().addHighlightedItem(fromAnchor_);
      ed.getFrame().getCanvas().repaint();
      cart_.getGUI().msg("start point selected");
      return null;
    }
    else {
      CartAction action = new CreateCorridorAction(ed, fromAnchor_, pt);
      ed.getFrame().getCanvas().removeHighlightedItem(fromAnchor_);
      fromAnchor_ = null;
      return action;
    }
  }

  public void splitCorridor(double x, double y) {
    /*Corridor c = cart_.getNetwork().getCorridorAtXY(x, y,
            cart_.getGUI().getCanvas().getClickToleranceW());

    if(c != null) {
      Point2D.Double pt = c.nearestPoint(x, y);
      AnchorPoint ap = newAnchorPoint(pt.x, pt.y);
      cart_.getNetwork().deleteCorridor(c);
      cart_.getNetwork().addCorridor(new Corridor(cart_.getNetwork().newCorridorID(), c.fPoint(), ap));
      cart_.getNetwork().addCorridor(new Corridor(cart_.getNetwork().newCorridorID(), ap, c.tPoint()));
    }*/
  }

  private CartAction newLineEvent(Editor ed) {
    String name = JOptionPane.showInputDialog(cart_.getGUI(), "Line name?");
    return new CreateLineAction(ed, name);
  }

  private CartAction modifyLineEvent(Editor ed, double x, double y) {
    Corridor c = ed.getDocument().getNetwork().getCorridorAtXY(x, y,
            ed.getFrame().getCanvas().getClickToleranceW());

    Line line = ed.getSelectedLine(); //cart_.getGUI().getControlPanel().getLineSelector().getSelectedLine();

    if(line == null) {
      cart_.getGUI().msg("No line selected");
      return null;
    }
    if(c == null) {
      cart_.getGUI().msg("No corridor selected");
      return null;
    }
    if(line.contains(c) && c != line.firstCorridor() && c != line.lastCorridor()) {
      cart_.getGUI().msg("Cannot remove selected corridor");
      return null;
    }
    if(line.getCorridors().size() > 1 &&  !c.adjacentTo(line.startPoint()) && !c.adjacentTo(line.endPoint())) {
      cart_.getGUI().msg("Invalid corridor selected");
      return null;
    }

    System.out.println("mod line "+line.getID());
    return new ModifyLineAction(ed, line, c);
  }

  private CartAction deleteLineEvent(Editor ed) {
    Line line = ed.getSelectedLine();
    return new DeleteLineAction(ed, line);
  }


  /*private CartAction editLinePropsEvent(Color color, int width, int offset, int month, int year) {
    Line line = //cart_.getGUI().getControlPanel().getLineSelector().getSelectedLine();
    return new EditLinePropertiesAction(line, color, width, offset, month, year);
  }*/

}
